package com.leyou.item.web;

import com.leyou.common.vo.PageResult;
import com.leyou.item.dto.BrandDTO;
import com.leyou.item.entity.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author fenglin
 * @date 2019/12/8 17:19
 */
@RestController
@RequestMapping("brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌
     * @param key
     * @param page 当前页
     * @param rows 每页显示数量
     * @param sortBy 排序字段
     * @param desc  是否降序
     * required：是否必须，默认是true
     * defaultValue：默认值，表示如果请求中没有同名参数时的默认值
     * value：参数名字，即入参的请求参数名字
     * @return
     */
    @GetMapping("/page")
    //key=&page=1&rows=5&sortBy=id&desc=false
    public ResponseEntity<PageResult<BrandDTO>> queryBrandByPage(
            @RequestParam(value = "key",required = false)String key,
            @RequestParam(value = "page",defaultValue = "1")Integer page,
            @RequestParam(value = "rows",defaultValue = "5")Integer rows,
            @RequestParam(value = "sortBy", defaultValue = "update_time") String sortBy,
            @RequestParam(value = "desc", defaultValue = "true") Boolean desc
    ){
        PageResult<BrandDTO> result = brandService.queryBrandByPage(key, page, rows, sortBy, desc);
        return ResponseEntity.ok(result);
    }

    /**
     * 新增品牌
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO){
        brandService.saveBrand(brandDTO);
        //返回值无
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 根据分类查询品牌
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategory(@RequestParam("id")Long id){
        return ResponseEntity.ok(brandService.queryBrandByCategory(id));
    }

    /**
     * 根据id查询品牌
     * @param id 品牌id
     * @return 品牌对象
     */
    @GetMapping("{id}")
    public ResponseEntity<BrandDTO> qureyBrandById(@PathVariable("id") Long id){
    return ResponseEntity.ok(brandService.queryBrandById(id));
    }
    /**
     * 根据品牌id集合查询品牌
     * @param brandIdList 品牌id
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds(@RequestParam("ids") List<Long> brandIdList){
        return ResponseEntity.ok(brandService.queryByIds(brandIdList));
    }
}
